<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Admin
 * Date: 03.09.13
 * Time: 11:07
 * To change this template use File | Settings | File Templates.
 */

class Grabber
{
    protected $_oBrowser = null;

    public function __construct()
    {
        $this->includeFile('AbstractParser');
        $this->includeFile('Proxy');
        $this->includeFile('Browser');

        $this->_oBrowser = new Browser();
    }

    public function getRow( $parser, $url = '' )
    {
        $this->includeFile('parsers/'.$parser);
        $oParser = new $parser;

        if($url)
            $oParser->setUrl($url);

	    if(is_array($oParser->getUrl()))
	    {
		    $aHtml = $this->_oBrowser->multiQuery( $oParser->getUrl() );

		    if(empty($aHtml))
			    return array();

		    $aResult = array();

		    foreach($aHtml as $url => $html)
		    {
			    $aResult[$url] = $oParser->getRow($html);
		    }

//		    print_r($aResult); exit;

		    return $aResult;
	    }

        $html = $this->_oBrowser->get( $oParser->getUrl() );

        return $oParser->getRow($html);
    }

    public function getRowset( $parser, $url = '', $nextPage = true )
    {
        $this->includeFile('parsers/'.$parser);

        $oParser = new $parser;
	    $oParser->setUrl($url);

        $html = $this->_oBrowser->get( $url );
//        echo $html; exit;

        $aResults = $oParser->getRowset($html);

	    yield $aResults;

	    unset($aResults);

        $aPages = $oParser->getPages($html);

	    $count = 0;
	    if(!empty($aPages))
	    {
		    $aParsedPages = $aPages;
		    do
		    {
			    $newPages = array();
			    $aHtml = $this->_oBrowser->multiQuery($aPages);
//print_r($aHtml); exit;
			    if(!empty($aHtml))
			    {
				    foreach($aHtml as $url => $html)
				    {
					    if(!is_string($html))
						    continue;

					    $aResult = $oParser->getRowset($html);
					    yield $aResult;

					    $aTmpPages = $oParser->getPages($html);

//					    print_r($aTmpPages);

					    $aParsedPages[$url] = $url;

//					    if(!empty($aResult))
//						    $aResults = array_merge($aResults, $aResult);

					    foreach($aTmpPages as $url)
					    {
						    if(!isset($aParsedPages[$url]))
						    {
							    $newPages[$url] = $url;
							    file_put_contents(PUBLIC_PATH.'/_urls.txt', $url."\n", FILE_APPEND);
							    file_put_contents(PUBLIC_PATH.'/_urls.txt', "\n", FILE_APPEND);
						    }
					    }
					    $count++;
				    }
			    }

			    $aPages = $newPages;
//print_r($newPages); exit;
		    }
		    while(!empty($newPages) && $count < 20000);
	    }

//        return $aResults;
    }

    public function includeFile($file)
    {
        $file = dirname(__FILE__) . '/grabber/' . $file . '.php';
        if (file_exists($file))
            require_once $file;
    }

}